package com.hh.bss.project.dao.impl;

import static com.hh.bss.util.ObjectUtils.isNotEmpty;

import java.util.ArrayList;
import java.util.List;

import com.hh.bss.auth.util.AuthSqlUtil;
import com.hh.bss.base.BaseHibernateDao;
import com.hh.bss.common.MapObjectRowMapper;
import com.hh.bss.common.page.Page;
import com.hh.bss.project.dao.AdvancePaymentDao;
import com.hh.bss.project.entity.AdvancePayment;
import com.hh.bss.project.vo.query.AdvancePaymentQuery;
import com.hh.bss.util.DateHelper;
import com.hh.bss.util.StringHelper;

public class AdvancePaymentDaoImpl extends BaseHibernateDao<AdvancePayment, Integer> implements AdvancePaymentDao<AdvancePayment, Integer> {

	@Override
	public Class getEntityClass() {
		return AdvancePayment.class;
	}

	@Override
	public Page findPage(AdvancePaymentQuery query) {
		Class[] os = { AdvancePayment.class };
		Class property = org.springframework.beans.BeanUtils.findPropertyType(query.getField_type(), os);
		if (property != null) {
			if ("java.lang.String".equals(property.getName())) {

				com.hh.bss.util.BeanUtils.setProperty(query, query.getField_type(), query.getQuery());

			} else if ("java.lang.Integer".equals(property.getName())) {

				com.hh.bss.util.BeanUtils.setProperty(query, query.getField_type(), StringHelper.parseInteger(query.getQuery(), 0));

			} else if ("java.lang.Double".equals(property.getName())) {
				com.hh.bss.util.BeanUtils.setProperty(query, query.getField_type(), StringHelper.parseDouble(query.getQuery(), 0));
			} else if ("java.util.Date".equals(property.getName())) {
				com.hh.bss.util.BeanUtils.setProperty(query, query.getField_type(), DateHelper.toDate(query.getQuery(), DateHelper.DEFAULT_DATE_FORMAT));
			}
		}

		StringBuffer sql2 = new StringBuffer();
		List values = new ArrayList();
		sql2.append("select * from ( ");
		sql2.append(" select a.id,a.cust_id custId,a.nbr,a.currency,a.branch_nbr branchNbr,a.amount,a.in_amount inAmount,a.out_amount outAmount,a.de_amount deAmount,a.balance,a.comment,a.status,a.creator,a.create_dt createDt ");
		sql2.append(" ,a1.companyId,a1.custName,a2.name branchCompanyName,a3.percentSum from advance_payment a ");
		sql2.append(" left join (select b.id,b.company_id companyId,c.abbr_name custName from customer b,company c where  b.company_id=c.id ) a1 on a.cust_id=a1.id  ");
		sql2.append(" left join system_branch_company a2 on a.branch_nbr=a2.branch_nbr  ");
		sql2.append(" left join (select a.ap_id apId,ifnull(sum(a.percent),0) percentSum from customer_partner a group by a.ap_id ) a3 on a3.apId = a.id ");
		sql2.append(" ) t where 1=1 ");
		if (isNotEmpty(query.getCustId())) {
			sql2.append(" and  t.custId = ? ");
			values.add(query.getCustId());
		}
		if (isNotEmpty(query.getNbr())) {
			sql2.append(" and  t.nbr like ? ");
			values.add("%" + query.getNbr() + "%");
		}
		if (isNotEmpty(query.getBranchNbr())) {
			sql2.append(" and  t.branchNbr = ? ");
			values.add(query.getBranchNbr());
		}
		if (isNotEmpty(query.getCustName())) {
			sql2.append(" and  t.custName like ? ");
			values.add("%" + query.getCustName() + "%");
		}
		if (isNotEmpty(query.getAmount())) {
			sql2.append(" and  t.amount = ? ");
			values.add(query.getAmount());
		}

		if (isNotEmpty(query.getInAmount())) {
			sql2.append(" and  t.inAmount = ? ");
			values.add(query.getInAmount());
		}
		if (isNotEmpty(query.getOutAmount())) {
			sql2.append(" and  t.outAmount = ? ");
			values.add(query.getOutAmount());
		}
		if (isNotEmpty(query.getDeAmount())) {
			sql2.append(" and  t.deAmount = ? ");
			values.add(query.getDeAmount());
		}
		if (isNotEmpty(query.getBalance())) {
			sql2.append(" and  t.balance = ? ");
			values.add(query.getBalance());
		}

		/*
		 * //增加权限控制 if (query.getOnlineUser()!=null){ String
		 * authsql=AuthSqlUtil.authPnSQL(query.getOnlineUser().getUsername(),
		 * "t.creator"); if (!StringHelper.isEmpty(authsql))
		 * sql2.append(authsql); }
		 */

		if (isNotEmpty(query.getSort())) {
			sql2.append(" order by t.").append(query.getSort());
			if (isNotEmpty(query.getDir())) {
				sql2.append(" ").append(query.getDir());
			}
		} else {
			sql2.append(" order by t.createDt desc ");
		}
		
		log.info("查询SQL:"+sql2.toString());

		return this.getJdbcDao().createPage(sql2.toString(), values.toArray(), new MapObjectRowMapper(), query.getPageSize(), query.getPageNumber());
	}

}
